﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Web;
using Antu.WorkFlowFactory;
using System.Data;
using EluZSProvider;
using System.Configuration;

namespace WebSys.UI.OfficeFlow
{
    public class FlowlistSkin : MPBasicTemplate
    {
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        DropDownList flowdownlist, ddlyxj;
        Button btnSearch;
        TextBox txtNo, txtCom;
        //MPRepeater MPRepeater1;
        Antu.WebControls.CustomGridView gv;
        //int currentPage = 1, pageSize = 10;
        protected string UserId
        {
            get { if (base.Page.Session["UserId"] != null) { return base.Page.Session["UserId"].ToString(); } else { return string.Empty; } }
            set { base.Page.Session["UserId"] = value; }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            InitControl();
            //if (!string.IsNullOrEmpty(base.Page.Request["page"]))
            //{
            //    currentPage = Convert.ToInt32(base.Page.Request["page"]);
            //}
            if (!Page.IsPostBack)
            {
                if (!_EluZSDataProvider.IsZSAdmin(UserId) && !_EluZSDataProvider.IsNetZSAdmin(UserId))
                {
                    Antu.Utilities.UtilitiesPage.AlertDialogClear("没有权限查看！", "");
                }
                else
                {
                    string straction = base.Page.Request["action"];
                    if (!string.IsNullOrEmpty(straction) && straction == "del")
                    {
                        string striid = base.Page.Request["iid"];
                        if (!string.IsNullOrEmpty(striid))
                        {
                          
                            string sql = @"select res_value from st_dynamic_resource where type=2 and iid='" + striid + "'";
                            DataTable dt = _EluZSDataProvider.GetDTTable(sql);
                            if (dt.Rows.Count > 0)
                            {
                                for (int i = 0; i < dt.Rows.Count; i++)
                                {
                                    _EluZSDataProvider.DeleteXmByID("st_attachment", "aid", dt.Rows[i]["res_value"].ToString());
                                }
                            }
                            _EluZSDataProvider.DeleteXmByID("st_dynamic_resource", "iid", striid);
                            dt = _FlowFactoryProvider.GetOneDs("st_form_input", "distinct DB_TABLE", "").Tables[0];
                            if (dt.Rows.Count > 0)
                            {
                                for (int i = 0; i < dt.Rows.Count; i++)
                                {
                                    string formTableName = dt.Rows[i]["DB_TABLE"].ToString();
                                    if (!string.IsNullOrEmpty(formTableName))
                                    {
                                        if (Convert.ToInt32(_FlowFactoryProvider.GetStr("select count(*) from user_tab_columns where table_name=Upper('"+formTableName+"') and column_name=Upper('iid')"))> 0)
                                        {
                                            if (Convert.ToInt32(_FlowFactoryProvider.GetOneStrString(formTableName, "count(*)", "iid", striid)) > 0)
                                            {
                                                _EluZSDataProvider.DeleteXmByID(formTableName, "iid", striid);
                                            }
                                        }
                                        
                                    }
                                }
                            }

                            _EluZSDataProvider.DeleteXmByID("st_work_item", "iid", striid);
                            _EluZSDataProvider.DeleteXmByID("st_suspend", "iid", striid);
                            _EluZSDataProvider.DeleteXmByID("st_work_item_hist", "iid", striid);
                            _EluZSDataProvider.DeleteXmByID("xt_instance_ext", "iid", striid);

                            _EluZSDataProvider.DeleteXmByID("st_instance", "iid", striid);
                            //Antu.Utilities.UtilitiesPage.AlertDialog("删除成功!", string.Format("window.location.href('Flowlist.aspx?page={0}');", base.Page.Request["page"]));
                            Antu.Utilities.UtilitiesPage.AlertDialog("删除成功!", "window.location.href('Flowlist.aspx');");
                        }
                    }
                    BindDll();
                    Bind();
                }
            }
        }
        private void InitControl()
        {
            flowdownlist = (DropDownList)FindControl("flowdownlist");
            ddlyxj = (DropDownList)FindControl("ddlyxj");
            txtNo = (TextBox)FindControl("txtNo");
            txtCom = (TextBox)FindControl("txtCom");
            btnSearch = (Button)FindControl("btnSearch");
            //MPRepeater1 = (MPRepeater)FindControl("MPRepeater1");
            btnSearch.Click += new EventHandler(btnSearch_Click);
            gv = (CustomGridView)FindControl("gv");
            gv.OnLoadData += new EventHandler(gv_OnLoadData);
            gv.RowDataBound += new GridViewRowEventHandler(gv_RowDataBound);
        }

        void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //判断案子是否已办结办结则不能被删除
             if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView rowview = (DataRowView)e.Row.DataItem;
                string striid = rowview["iid"].ToString();
                string status = _EluZSDataProvider.GetString("select status from st_instance where iid='" + striid + "'");
                if (status == "2")
                {
                    e.Row.Cells[8].Text = "删除";
                }
             }            
            
        }

        void gv_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Bind();
        }
        protected void BindDll()
        {
            //DataTable dt = _FlowFactoryProvider.GetStartWorkFlowListByUserId(UserId);
            string ajlx = ConfigurationManager.AppSettings["ZSAnJianType"].ToString();
            DataTable dt = _FlowFactoryProvider.NewGetStartWorkFlowListByUserId(UserId, ajlx);
            if (!(dt.Rows.Count > 0))
            {
                if (_EluZSDataProvider.IsZSAdmin(UserId))
                {
                    dt = _FlowFactoryProvider.GetOneDs("st_workflow", "wid,wname", " and  wname in ("+ajlx+") and rot=0").Tables[0];
                }
            }
            DataRow dr = dt.NewRow();
            dr["WNAME"] = "全部";
            dr["wid"] = "";
            dt.Rows.InsertAt(dr, 0);
            flowdownlist.DataSource = dt;
            flowdownlist.DataTextField = "WNAME";
            flowdownlist.DataValueField = "wid";
            flowdownlist.DataBind();
        }
        protected void Bind()
        {
            string ajlx = ConfigurationManager.AppSettings["ZSAnJianType"].ToString();
            string strype = string.Empty;
            string wid = flowdownlist.SelectedValue;
            string yxj = ddlyxj.SelectedValue;
            string iid = txtNo.Text.Trim();
            string strcom = txtCom.Text.Trim();
            if (!string.IsNullOrEmpty(wid)) { strype += "and 业务类型 in (select wname from st_workflow where wid='" + wid + "')"; }
            if (!string.IsNullOrEmpty(yxj)) { strype += "and 优先级 ='" + yxj + "'"; }
            if (!string.IsNullOrEmpty(iid)) { strype += "and iid like '%" + iid + "%'"; }
            if (!string.IsNullOrEmpty(strcom)) { strype += "and 申请单位 like '%" + strcom + "%'"; }
            string tablename = @"( select i.iid, w.wname 业务类型,i.name 申请单位,(select 联系人 from xt_instance_ext where iid=i.iid) 联系人,
to_char(i.EXBEGINMONITOR,'yyyy-mm-dd hh24:mi:ss') 接件时间,i.priority 优先级,i.status from st_instance i,st_workflow w where w.wid=i.wid  and w.wname in("+ajlx+") and rownum<1000)";
            //string sql = MPRepeater.OraPageListSql(currentPage, pageSize, "t.*", tablename, "iid", strype, "trim(t.iid) desc");
            string sql="select t.* from "+tablename+" t where 1=1 "+strype+" order by trim(t.iid) desc";
            DataTable dt = _EluZSDataProvider.GetDTTable(sql);
            gv.DataSource = dt;
            gv.RecordCount = dt.Rows.Count;
            gv.DataBind();

            //MPRepeater1.DataSource = dt;
            //MPRepeater1.DataBind();

            //sql = MPRepeater.OraPageListSql(tablename, "iid", strype);
            //MPRepeater1.PageCount = Convert.ToInt32(_EluZSDataProvider.GetDTColumn(sql));
            //MPRepeater1.PageSize = pageSize;
            //MPRepeater1.PageIndex = currentPage;
            //MPRepeater1.PageUrl = "?page={0}" + "&typename=" + base.Page.Request["typename"];
            //Label pagestr = (Label)FindControl("pagestr");
            //pagestr.Text = MPRepeater1.GetPageList();
        }



    }
}


